package io.renren.common.validator;

import io.renren.common.exception.RRException;
import org.apache.commons.lang.StringUtils;

import java.util.regex.Pattern;

/**
 * 数据校验
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-23 15:50
 */
public abstract class Assert {

    public static void isBlank(String str, String message) {
        if (StringUtils.isBlank(str)) {
            throw new RRException(message);
        }
    }

    public static void isNull(Object object, String message) {
        if (object == null) {
            throw new RRException(message);
        }
    }

    public static void passwordVerify(String str){
        String pattern = "^(?![a-zA-Z]+$)(?![0-9]+$)[0-9A-Za-z]{8,16}$";
        boolean isMatch = Pattern.matches(pattern,str);
        if (!isMatch){
            throw new RRException("密码必须同时包含数字和字母，不能包含特殊字符(最少8位,最多16位)");
        }
    }

    public static void phoneVerify(String str){
        String pattern = "1[3|4|5|7|8][0-9]\\d{8}";
        boolean isMatch = Pattern.matches(pattern,str);
        if (!isMatch){
            throw new RRException("电话格式错误");
        }
    }
}
